import sys
import os
import time
import math
import shlex, subprocess,sys
from evaluations import *


data_dir="./data";
trn_x_file=data_dir+"/regression/housing_trn_x.libtm";
trn_y_file=data_dir+"/regression/housing_trn_y.libtm";
tst_x_file=data_dir+"/regression/housing_tst_x.libtm";
tst_y_file=data_dir+"/regression/housing_tst_y.libtm";

trn_x_file=data_dir+"/classification/poker_trn_x.libtm";
trn_y_file=data_dir+"/classification/poker_trn_y.libtm";
tst_x_file=data_dir+"/classification/poker_tst_x.libtm";
tst_y_file=data_dir+"/classification/poker_tst_y.libtm";

model_file=data_dir+"/model/dna.model";
pred_file=data_dir+"/prediction/dna.pred";

#parameters
alg="RF";
split_criterion="gini";# using "mse" for regression, "gini" or "entropy" for classification
n_jobs=1;
n_trees=100;
max_depth=999; # let forest full grow
min_sample_leaf=5;
max_features_ratio=1;
bootstrap=1;
oob=0;
verbose=1;
compute_importance=0;


cmdline="./TE_train -alg %s -n_jobs %d -split_criterion %s -n_trees %d -max_depth %d -min_sample_leaf %d -max_features_ratio %f -train_x_fn %s -train_y_fn %s -validation_x_fn %s -validation_y_fn %s -model_fn %s -compute_importance %d -bootstrap %d -oob %d -verbose %d" % (alg,n_jobs,split_criterion,n_trees,max_depth,min_sample_leaf,max_features_ratio,trn_x_file,trn_y_file,tst_x_file,tst_y_file,model_file,compute_importance,bootstrap,oob,verbose);

pred_cmdline="./TE_predict -model_fn %s -test_x_fn %s -prediction_fn %s" %(model_file,tst_x_file,pred_file);
#print cmdline

#train Random Forest
args=shlex.split(cmdline);
status= subprocess.call(args);
if status!=0:
	sys.exit(0);

#prediction
args=shlex.split(pred_cmdline);
status=subprocess.call(args);

if status!=0:
	sys.exit(0);

tst_y=[];
tst_y_input=open(tst_y_file,'r');
for line in tst_y_input:
	tst_y.append(float(line.strip().split()[0]));

pred=[];
pred_input=open(pred_file,'r');
for line in pred_input:
	pred.append(float(line.strip().split()[0]));


[tst_acc,tst_mse,dum]=evaluations(pred,tst_y);
#print 'test RMSE=%f,Acc=%f' %(tst_mse**0.5,tst_acc)



